<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\BaseController;

use App\Models\Role;
use App\Models\Admin as User;
use Illuminate\Support\Facades\DB;

class UserController extends BaseController
{
    protected $fields = [
        'account_number' => '',
        'roles' => []
    ];

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $admins = DB::table('admins')->paginate(15);
        return view('admin.user.index', compact('admins', $admins));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = [];
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }
        $data['rolesAll'] = Role::all()->toArray();
        return view('admin.user.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user = new User();
        foreach (array_keys($this->fields) as $field) {
            $user->$field = $request->get($field);
        }
        if ($request->get('account_number') != '' && $request->get('password') != '' && $request->get('repassword') != ''
            && $request->get('password') == $request->get('repassword')
        ) {
            $user->password = bcrypt($request->get('password'));
            $user->active = 1;
            $user->create_time = time();
            $user->update_time = time();
        } else {
            return redirect()->back()->withErrors('用户名或密码不能为空！');
        }
        unset($user->roles);
        $user->save();
        if (is_array($request->get('roles'))) {
            $user->giveRoleTo($request->get('roles'));
        }
        return redirect('admin/user')->withSuccess('添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::find((int)$id);
        if (!$user) return redirect('admin/user')->withErrors('该用户不存在');
        $roles = [];
        if ($user->roles) {
            foreach ($user->roles as $v) {
                $roles[] = $v->id;
            }
        }
        $user->roles = $roles;
        foreach (array_keys($this->fields) as $field) {
            $data[$field] = old($field, $user->$field);
        }
        $data['rolesAll'] = Role::all()->toArray();
        $data['id'] = (int)$id;
        return view('admin.user.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $user = User::find((int)$id);
        foreach (array_keys($this->fields) as $field) {
            $user->$field = $request->get($field);
        }
        //当密码和重复密码留空时则不更新用户密码
        if ($request->get('password') == '' && $request->get('repassword') == '') {
            $user->update_time = time();
        } else {
            if ($request->get('account_number') != '' && $request->get('password') != ''
                && $request->get('repassword') != '' &&
                $request->get('password') == $request->get('repassword')
            ) {
                $user->password = bcrypt($request->get('password'));
                $user->update_time = time();
            } else {
                return redirect()->back()->withErrors('用户名不为空或两次输入密码不一致！');
            }
        }
        unset($user->roles);
        $user->save();

        $user->giveRoleTo($request->get('roles', []));
        return redirect('admin/user')->withSuccess('编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $user = User::find((int)$id);
        if ($user && $user->id != 1) {
            $user->delete();
        } else {
            return redirect()->back()->withErrors('删除失败');
        }
        return redirect()->back()->withSuccess('删除成功');
    }
}
